package lormi.meipin.service.impl;


import lormi.meipin.entity.City;
import lormi.meipin.service.BaseService;
import lormi.meipin.service.ICityService;
import lormi.meipin.util.CacheUtil;
import lormi.meipin.util.PingYinUtil;
import lormi.meipin.vo.CityVo;
import org.springframework.stereotype.Service;

import java.util.*;

@SuppressWarnings("unchecked")
@Service
public class CityService extends BaseService implements ICityService{

	@Override
	public List<City> getAllProvinces() {
		
		List<City> list = (List<City>) CacheUtil.getCacheUtil().getCityCache("getAllProvinces");
		if (list==null) {
			list = cityMapper.selectAllProvinces();
			CacheUtil.getCacheUtil().setCityCache("getAllProvinces", list);
		}
		return list;
	}

	@Override
	public City getByCityName(String name) {
		return cityMapper.selectByName(name);
	}

	@Override
	public List<City> getAllCityByPrivince(String privinceid) {

		List<City> list = (List<City>) CacheUtil.getCacheUtil().getCityCache("getAllCityByPrivince_"+privinceid);
		if (list==null) {
			list = cityMapper.selectAllCityInOneProvince(privinceid);
			CacheUtil.getCacheUtil().setCityCache("getAllCityByPrivince_"+privinceid, list);
		}
		return list;
	}

	@Override
	public List<City> getAllDistrictByCityId(String cityid) {

		List<City> list = (List<City>) CacheUtil.getCacheUtil().getCityCache("getAllDistrictByCityId"+cityid);
		if (list==null) {
			list = cityMapper.selectAllDistrictInOneCity(cityid);
			CacheUtil.getCacheUtil().setCityCache("getAllDistrictByCityId"+cityid, list);
		}
		return list;
	}

	@Override
	public List<CityVo> getAllCitySortAZ() {

		List<CityVo> cityVo = (List<CityVo>) CacheUtil.getCacheUtil().getCityCache("getAllCitySortAZ");
		if (cityVo==null) {
			List<City> city = cityMapper.selectAllCity();
			cityVo = new ArrayList<>();

			for(City c:city){
				String str = PingYinUtil.getFirstSpell(c.getName());
				if("长".equals(c.getName().substring(0, 1))){
					str = "c";
				}
				if("重".equals(c.getName().substring(0, 1))){
					str = "c";
				}
				if("厦".equals(c.getName().substring(0, 1))){
					str = "x";
				}
				cityVo.add(new CityVo(str,c));
			}
			Collections.sort(cityVo);
			CacheUtil.getCacheUtil().setCityCache("getAllCitySortAZ", cityVo);
		}
		return cityVo;
	}

	@Override
	public Map<String,List<City>> getAllerCitySortAZ(){
		
		Map<String,List<City>> map = (Map<String,List<City>>) CacheUtil.getCacheUtil().getCityCache("getAllerCitySortAZ");
		if (map==null) {
			List<City> city = cityMapper.selectAllCity();
			map = new HashMap<>();
			for(City c:city){
				String first = PingYinUtil.getFirstSpell(c.getName()).toUpperCase();
				if("长".equals(c.getName().substring(0, 1))){
					first = "C";
				}
				if("重".equals(c.getName().substring(0, 1))){
					first = "C";
				}
				if("厦".equals(c.getName().substring(0, 1))){
					first = "X";
				}
				if(map.get(first)!=null){
					List<City> cs = map.get(first);
					cs.add(c);
					map.put(first,cs);
				}else{
					List<City> cs = new ArrayList<>();
					cs.add(c);
					map.put(first, cs);
				}
			}
			CacheUtil.getCacheUtil().setCityCache("getAllerCitySortAZ", map);
		}
		return map;
	}

	@Override
	public List<City> getHotCity() {
		
		List<City> list = (List<City>) CacheUtil.getCacheUtil().getCityCache("getHotCity");
		if (list==null) {
			list = new ArrayList<>();
			list.add(new City(2,"北京市"));
			list.add(new City(1834,"上海市"));
			list.add(new City(4996,"广州市"));
			list.add(new City(5883,"成都市"));
			list.add(new City(5199,"深圳市"));
			list.add(new City(2076,"南京市"));
			list.add(new City(2450,"杭州市"));
			list.add(new City(5717,"重庆市"));
			CacheUtil.getCacheUtil().setCityCache("getHotCity", list);
		}
		return list;
	}
	
	
}
